package com.homeworkitem.service;

import com.homeworkitem.entity.Flower;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

/**
 * 描述: 鲜花管理类
 *
 * @author CodeYang
 * @version 2020/12/17 11:23
 */
public class FlowerMgr {
    private Flower[] flowerMgrObj = new Flower[5];

    private int size = flowerMgrObj.length;

    /**
     * 构造函数 -初始化对象数组
     */
    public FlowerMgr() {
        this.inital();
    }

    /**
     * 初始化数组
     */
    private void inital() {
        flowerMgrObj[0] = new Flower("005", "玫瑰花", 80, 20.0, "2020-12-01", "小王1", "情人节专卖");
        flowerMgrObj[1] = new Flower("002", "月季花", 99, 28.0, "2020-12-05", "小王2", "一般品");
        flowerMgrObj[2] = new Flower("003", "桔梗花", 90, 95.0, "2020-12-04", "小王3", "一般品");
        flowerMgrObj[3] = new Flower("001", "百合花", 70, 17.0, "2020-12-02", "小王4", "一般品");
        flowerMgrObj[4] = new Flower("004", "向日葵花", 80, 20.0, "2020-12-03", "小王5", "阳光用户");
    }

    /**
     * 根据订单编号判断数组 元素是否存在
     *
     * @param number Flower[] arr 数组 str 订单号
     * @return 存在返回 目标索引 不存在返回-1
     */
    private int getArrByIndexIsNull(Flower[] arr, String number) {
        int index = -1;
        for (int i = 0; i < size; i++) {
            if (arr[i].getNum().equals(number)) {
                index = i;
                break;
            }
        }

         /*   if (index == -1) {
                throw new IllegalArgumentException("指定订单编号不存在");
            }*/

        return index;
    }

    /**
     * 打印 目标数组的指定元素
     *
     * @param arr
     * @param index
     */
    private void printfArrForIndex(Flower[] arr, int index)  {
        System.out.println("==============================================================================");
        System.out.println("编号:\t名称\t\t销售数量\t价格\t\t日期\t\t\t销售员\t备注");
        System.out.println(arr[index].getNum() + "\t\t" + arr[index].getName() + "\t" + arr[index].getSalesVolume() + "\t\t" + arr[index].getPrice() + "\t" + arr[index].getDate() + "\t" + arr[index].getSellerPerson() + "\t\t" + arr[index].getRemarks());
        System.out.println("==============================================================================");

    }


    /**
     * 功能面板
     */
    public void mainMenu() {
        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("=======================欢迎关联七色花销售管理系统==================================");
            System.out.println("1:查询销售订单");
            System.out.println("2:修改销售订单");
            System.out.println("3:删除销售订单");
            System.out.println("4:添加销售订单");
            System.out.println("0:退出系统");
            System.out.println("==============================================================================");
            int chose = input.nextInt();
            switch (chose) {
                case 1:
                    // 查询订单
                    this.searchAll();
                    break;
                case 2:
                    //修改订单
                    this.updateSale();
                    break;
                case 3:
                    // 删除订单--根据编号
                    this.deleteSale();
                    break;
                case 4:
                    // 删除订单--根据编号
                    this.addSale();
                    break;
                case 0:
                    System.out.println("系统退出!!!!谢谢您的使用");
                    System.exit(0);
                default:
                    throw new IllegalArgumentException("输入不合法!!!!");
            }
        }

    }


    /**
     * 查询订单
     */
    public void searchAll() {
        System.out.println("==========订单详细信息如下:======================================================");
        System.out.println("编号:\t名称\t\t销售数量\t价格\t\t日期\t\t\t销售员\t备注");
        /* 根据订单号排序*/
        Arrays.sort(flowerMgrObj, 0, size, new Comparator<Flower>() {
            @Override
            public int compare(Flower o1, Flower o2) {

                return o1.getNum().compareTo(o2.getNum());
            }
        });
        for (Flower flower : flowerMgrObj) {
            // 判断对象是否为null 不为null 输出
            if (flower != null) {
                String str = flower.getNum() + "\t\t" + flower.getName() + "\t" + flower.getSalesVolume() + "\t\t" + flower.getPrice() + "\t" + flower.getDate() + "\t" + flower.getSellerPerson() + "\t\t" + flower.getRemarks();
                System.out.println(str);
            }

        }
        System.out.println("==============================================================================");
    }


    /**
     * 删除订单
     */
    public void deleteSale() {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入要删除的订单编号:");
        String num = input.next();

        int index = this.getArrByIndexIsNull(flowerMgrObj, num);
        if (index==-1){
            System.out.println("订单编号不存在,无法删除,退回");
            return;
        }

        System.out.println("您要删除的订单详细信息如下:");
        /* 根据订单号打印订单信息*/
        printfArrForIndex(flowerMgrObj, index);

        System.out.println("确定要删除吗?Y/N");
        String flag = input.next();
        if (flag.equalsIgnoreCase("Y")) {
            //  删除 指定元素
            reArrItemForIndex(this.flowerMgrObj, index);
        }
        this.searchAll();


    }


    /**
     * 根据索引删除数组元素
     *
     * @param index
     */
    private void reArrItemForIndex(Object[] arr, int index) {
        // 1非空判断
        if (null == arr) {
            throw new IllegalArgumentException("数组对象不能为空");
        }
        // 2 判断索引范围是否在数组中
        if (index < 0 || index > arr.length) {
            throw new ArrayIndexOutOfBoundsException("数组索引越界或不在范围");
        }
        // 3 位移删除数组索引
        int len = arr.length;
        // 将删除元素后的索引位置 向前移动一位 index - len -1
        for (int i = index; i < len - 1; i++) {
            arr[i] = arr[i + 1];
        }
        arr[len - 1] = null;
        // 控制size 大小
        size--;

    }

    /**
     * 修改订单--- 修改订单还是要根据索引来修改
     */
    public void updateSale() {

        Scanner input = new Scanner(System.in);
        System.out.println("请输入销售编号:");
        String num = input.nextLine();


        int index = getArrByIndexIsNull(flowerMgrObj, num);
        if (index == -1){
            System.out.println("要修改订单编号不存在,退回");
            return;
        }
        // 获得编号对象
        System.out.println("您要修改的订单信息如下:\n");
        printfArrForIndex(flowerMgrObj, index);

        System.out.println("输入修改后的鲜花名称");
        String name = input.next();
        System.out.println("输入修改后的销售数量");
        int salesVolume = input.nextInt();
        System.out.println("输入修改后的价格");
        double price = input.nextDouble();
        System.out.println("输入修改后的日期");
        String date = input.next();
        System.out.println("输入修改后的销售人员");
        String sellerPerson = input.next();
        System.out.println("输入修改后的备注");
        String remarks = input.next();
        System.out.println("您确定要修改吗?Y/N");
        String flag = input.next();
        if (flag.equalsIgnoreCase("Y")) {
            flowerMgrObj[index].setName(name);
            flowerMgrObj[index].setSalesVolume(salesVolume);
            flowerMgrObj[index].setPrice(price);
            flowerMgrObj[index].setDate(date);
            flowerMgrObj[index].setSellerPerson(sellerPerson);
            flowerMgrObj[index].setRemarks(remarks);
        }
        if (flag.equalsIgnoreCase("N")) {
            System.out.println("退出修改");
        }
        this.searchAll();
    }


    /**
     * 启动类
     */
    public void startUp() {

        try {
            this.mainMenu();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        FlowerMgr flowerMgr = new FlowerMgr();
        flowerMgr.searchAll();
        flowerMgr.addSale();
        flowerMgr.searchAll();
    }

    /**
     * 新增订单功能
     *
     * @secin V0.1  增加到 数组元素的最后一位
     */
    private void addSale() {
        System.out.println("输入新增信息:");
        Scanner input = new Scanner(System.in);
        System.out.println("输入订单编号");

        String number = input.next();

        if (-1 != getArrByIndexIsNull(flowerMgrObj, number)) {
            System.out.println("订单编号已存在");
            return;
        }


        System.out.println("输入鲜花名称");
        String name = input.next();
        System.out.println("输入销售数量");
        int salesVolume = input.nextInt();
        System.out.println("输入价格");
        double price = input.nextDouble();
        System.out.println("输入日期");
        String date = input.next();
        System.out.println("输入销售人员");
        String sellerPerson = input.next();
        System.out.println("输入备注");
        String remarks = input.next();
        int len = flowerMgrObj.length - 1;

        if (flowerMgrObj[len - 1] == null) {
            flowerMgrObj[len - 1] = new Flower(number, name, salesVolume, price, date, sellerPerson, remarks);
        } else {
            // 扩容  -- 没有删除元素的情况下
            Flower[] flowerMgrObj02 = Arrays.copyOf(flowerMgrObj, len + 1);
            // 把堆内存地址赋值给原对象地 数据值--地址值
            flowerMgrObj = flowerMgrObj02;
            System.out.println("编号:\t名称\t\t销售数量\t价格\t\t日期\t\t\t销售员\t备注");
            flowerMgrObj02[len] = new Flower(number, name, salesVolume, price, date, sellerPerson, remarks);
        }
        this.searchAll();
    }
}
